package com.project.study.farming.base.manager;

import com.project.study.farming.base.entity.po.AreaPo;
import com.project.study.farming.base.mapper.DrawMapper;
import com.project.study.farming.common.utils.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DrawManager {

    private static final List<String> TITLE = new ArrayList<>();

    static {
        for(int i = 0; i <= 23; i++){
            TITLE.add(String.format("%d时", i));
        }
    }

    @Autowired
    private DrawMapper drawMapper;

    @Autowired
    private BeanMapper beanMapper;

    public Map<String, Object> drawPic(Integer id){
        List<Map<String, Object>> list = this.drawMapper.drawPic(id);
        List<String> crops = new ArrayList<>();
        List<Double> counts = new ArrayList<>();
        if(Objects.isNull(list) || list.isEmpty()){
            return null;
        }else{
            double num = 0;
            Map<String, List<Map<String, Object>>> resMap = list.stream().collect(Collectors.groupingBy(map -> map.get("cropName").toString()));
            resMap.forEach((k, v) -> {
                crops.add(k);
                counts.add(v.stream().mapToDouble(map -> Double.parseDouble(map.get("count").toString())).sum());
            });
        }
        AreaPo area = this.beanMapper.selectOne(AreaPo.class, String.format("id = %d", id));
        crops.add("可用面积");
        counts.add(area.getEnableSize());
        Map<String, Object> res = new HashMap<>(2);
        res.put("crops", crops);
        res.put("counts", counts);
        return res;
    }

    @SuppressWarnings("all")
    public Map<String, Object> drawLine(Integer id){
        Map<String, Object> res = new HashMap<>(4);
        List<Double> shiduList = new ArrayList<>();
        List<Double> wenduList = new ArrayList<>();
        List<Map<String, Object>> list = this.drawMapper.drowLine(id);
        if(Objects.isNull(list) || list.isEmpty()){
            return null;
        }else{
            Map<Object, List<Map<String, Object>>> resMap = list.stream().collect(Collectors.groupingBy(m -> m.get("createTime")));
            for(int i = 0; i <= 23; i++){
                String key = i < 10 ? String.format("0%d", i) : String.valueOf(i);
                List<Map<String, Object>> inList = resMap.get(key);
                if(Objects.isNull(inList) || inList.isEmpty()){
                    shiduList.add(0.0);
                    wenduList.add(0.0);
                }else{
                    double shidu = inList.stream().map(m -> m.get("temperature").toString())
                            .mapToDouble(Double :: parseDouble).average().getAsDouble();
                    double wen = inList.stream().map(m -> m.get("dampness").toString()).
                            mapToDouble(Double :: parseDouble).average().getAsDouble();
                    shiduList.add(shidu);
                    wenduList.add(wen);
                }
            }
        }
        res.put("shiduList", shiduList);
        res.put("wenduList", wenduList);
        res.put("title", TITLE);
        return res;
    }
}
